package com.taotao.sso.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import Utils.CookieUtils;

import com.taotao.sso.pojo.TbUser;
import com.taotao.sso.service.UserService;

@Controller
public class UserController {

	@Autowired
	private UserService userService;

	private static final String COOKIE_NAME = "_PHF_TOKEN";

	@RequestMapping(value = "register", method = RequestMethod.GET)
	public String showReg() {
		return "register";
	}

//	@RequestMapping(value = "login", method = RequestMethod.GET)
//	public String showlogin() {
//		return "login";
//	}
	@RequestMapping(value = "login", method = RequestMethod.GET)
	public ModelAndView showlogin(@RequestParam("returnUrl")String url) {
		ModelAndView mv = new ModelAndView("login");
		mv.addObject("returnUrl", url);
		System.out.println(url);
		return mv;
	}


	/**
	 * 校验用户信息是否已经注册
	 * 
	 * @param param
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "user/check/{param}/{type}", method = RequestMethod.GET)
	public ResponseEntity<Boolean> validateUser(
			@PathVariable("param") String param,
			@PathVariable("type") Integer type) {
		try {
			Boolean bool = userService.check(param, type);
			if (null == bool) {
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
			}
			return ResponseEntity.ok(bool);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
				null);
	}

	/**
	 * 注册用户信息
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "user/doRegister", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> doRegister(TbUser user) {

		Map<String, String> status = new HashMap<String, String>();

		try {
			Boolean state = userService.save(user);
			if (state) {
				status.put("status", "200");
			} else {
				status.put("status", "400");
			}
			return status;
		} catch (Exception e) {
			status.put("status", "500");
			e.printStackTrace();
		}
		return status;
	}

	/**
	 * 登录
	 * @param user
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "user/doLogin", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> doLogin(TbUser user, 
									   HttpServletRequest request,
									   HttpServletResponse response,
									  @RequestParam("returnUrl")String returnUrl) {

		Map<String, String> status = new HashMap<String, String>();
		 
		try {
			String token = userService.doLogin(user);
			if (token != null) {
				status.put("status", "200");
				status.put("returnUrl", returnUrl);
				CookieUtils.setCookie(request, response, COOKIE_NAME, token);
				//response.sendRedirect(returnUrl);
			} else {
				status.put("status", "400");
			}
			return status;
		} catch (Exception e) {
			status.put("status", "500");
			e.printStackTrace();
		}
		return status;
	}
//	@RequestMapping(value = "user/doLogin", method = RequestMethod.POST)
//	@ResponseBody
//	public String doLogin1(TbUser user, 
//									   HttpServletRequest request,
//									   HttpServletResponse response,
//									  @RequestParam("returnUrl")String returnUrl) {
//
//		Map<String, String> status = new HashMap<String, String>();
//		 
//		try {
//			String token = userService.doLogin(user);
//			if (token != null) {
//				status.put("status", "200");
//				CookieUtils.setCookie(request, response, COOKIE_NAME, token);
//				return "redirect:"+returnUrl;
//				//response.sendRedirect(returnUrl);
//			} else {
//				status.put("status", "400");
//			}
//			 return "redirect:"+returnUrl;
//		} catch (Exception e) {
//			status.put("status", "500");
//			e.printStackTrace();
//		}
//		 return "redirect:"+returnUrl;
//	}

	/**
	 * 根据token查询用户信息
	 * @param token
	 * @return
	 */
	@RequestMapping(value = "user/{token}", method = RequestMethod.GET)
	public ResponseEntity<TbUser> queryToken(@PathVariable("token") String token) {
		try {
			TbUser user = userService.queryUserByToken(token);
			if (user != null) {
			     return ResponseEntity.ok(user);
			}
			else 
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
}
